﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebFatura
{
    public partial class Fatura : System.Web.UI.Page
    {
        Mikro mikro = new Mikro();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["userno"] == null)
            {
                Response.Redirect("/Default.aspx");
            }

            txtSeri.Text = Session["seri"].ToString();

            dteTarih.Date = DateTime.Now;
            Cari cr = Session["cari"] as Cari;
            if (cr != null)
            {
                txtCariAdi.Text = cr.Unvani;
            }

            ASPxGridView2.DataSource = mikro.StokListe();
            ASPxGridView2.DataBind();
            ASPxGridView2.KeyFieldName = "ID";

            if (!IsPostBack)
            {
                if (Request.QueryString.Count > 0)
                {
                    string seri = Request.QueryString["seri"];
                    int sira = Convert.ToInt32(Request.QueryString["sira"]);
                    EvrakOku(seri, sira);
                }
            }


            if (Session["lines"] != null)
            {
                ASPxGridView1.DataSource = Session["lines"] as List<Line>;
                ASPxGridView1.DataBind();
            }
            else
            {
                List<Line> lines = new List<Line>();
                Session["lines"] = lines;
                ASPxGridView1.DataSource = lines;
                ASPxGridView1.DataBind();
            }

            ASPxGridView2.Settings.ShowVerticalScrollBar = true;
            ASPxGridView2.Settings.VerticalScrollBarStyle = DevExpress.Web.ASPxGridView.GridViewVerticalScrollBarStyle.Standard;
            ASPxGridView2.Settings.ShowHorizontalScrollBar = true;
        }

        private void EvrakOku(String seri, int sira)
        {
            Session["lines"] = null;
            Evrak evr = mikro.EvrakOku(seri, sira);
            txtSeri.Text = evr.Seri;
            txtSira.Text = evr.Sira.ToString();
            txtAciklama.Text = evr.Aciklama;

            List<Line> lines = new List<Line>();
            foreach (HareketBilgileri item in evr.Rows)
            {
                lines.Add(new Line() { Adi = item.StokAdi, Fiyat = item.Fiyat, ID = item.stokID, KdvID = item.KdvSira, KdvOrani = item.KdvOrani, Kodu = item.StokKodu, Miktar = item.Miktar, Tutar = item.BurutTutar, sharID = item.sharID });
            }
            Session["lines"] = lines;
        }

        protected void ASPxGridView2_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
        {
            if (e.Parameters.IndexOf("ekle") > -1)
            {
                var sonuc = ASPxGridView2.GetRow(Convert.ToInt32(e.Parameters.Split(';')[1]));
                if (sonuc != null)
                {
                    Stok st = sonuc as Stok;
                    List<Line> lines = Session["lines"] as List<Line>;
                    var line = lines.Where(x => x.ID == st.ID).FirstOrDefault();

                    if (line != null)
                    {
                        line.Miktar = Convert.ToDouble(e.Parameters.Split(';')[2]);
                        line.Tutar = line.Miktar * line.Fiyat;
                    }
                    else
                    {
                        lines.Add(new Line() { Adi = st.Adi, Fiyat = st.Fiyat, ID = st.ID, Kodu = st.Kodu, Miktar = Convert.ToDouble(e.Parameters.Split(';')[2]), Tutar = st.Fiyat * Convert.ToDouble(e.Parameters.Split(';')[2]), KdvID = st.KdvID, KdvOrani = st.KdvOrani });
                    }
                }
            }
        }

        protected void ASPxGridView1_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
        {
            if (e.Parameters.IndexOf("guncelle") > -1)
            {
                var sonuc = ASPxGridView1.GetRow(Convert.ToInt32(e.Parameters.Split(';')[2]));
                if (sonuc != null)
                {
                    Line line = sonuc as Line;
                    if (line != null)
                    {
                        line.Miktar = Convert.ToDouble(e.Parameters.Split(';')[1]);
                        line.Tutar = line.Miktar * line.Fiyat;
                    }
                }
            }
            else if (e.Parameters.IndexOf("satirsil") > -1)
            {
                var sonuc = ASPxGridView1.GetRow(Convert.ToInt32(e.Parameters.Split(';')[1]));
                if (sonuc != null)
                {
                    Line line = sonuc as Line;
                    if (line != null)
                    {
                        List<Line> lines = Session["lines"] as List<Line>;
                        if (!string.IsNullOrEmpty(txtSira.Text))
                        {
                            if (Session["lines"] != null)
                            {
                                List<Line> remlines = Session["remlines"] as List<Line>;
                                remlines.Add(line);
                            }
                            else
                            {
                                List<Line> remlines = new List<Line>();
                                remlines.Add(line);
                                Session["remlines"] = remlines;
                            }
                        }
                        lines.Remove(line);
                    }
                }
            }
        }

        protected void btnKaydet_Click(object sender, EventArgs e)
        {
            evrakKayit();
            Response.Redirect("/Liste.aspx?tip=cariliste");
        }

        private void evrakKayit()
        {
            string evrakSeri = txtSeri.Text;
            Cari cr = Session["cari"] as Cari;

            string[] yazdirmaAciklama = new string[5];
            yazdirmaAciklama[0] = txtAciklama1.Text;
            yazdirmaAciklama[1] = txtAciklama2.Text;
            yazdirmaAciklama[2] = txtAciklama3.Text;
            yazdirmaAciklama[3] = txtAciklama4.Text;
            yazdirmaAciklama[4] = txtAciklama5.Text;

            Evrak evrak = new Evrak();
            evrak.EvrakAciklamalari = yazdirmaAciklama;
            evrak.EvrakCinsi = EvrakTipleri.SatisFaturasi;
            evrak.BelgeNo = "";
            evrak.CariKodu = cr.Kodu;
            evrak.CikisDepoNo = 0;
            evrak.GirisDepoNo = 0;
            evrak.MikroKullanici = Session["userno"].ToString();
            evrak.PlasiyerKodu = "";
            evrak.Seri = evrakSeri;
            if (!string.IsNullOrEmpty(txtSira.Text))
                evrak.Sira = Convert.ToInt32(txtSira.Text);
            evrak.SrmMerkezKodu = "";
            evrak.Tarih = new DateTime(dteTarih.Date.Year, dteTarih.Date.Month, dteTarih.Date.Day);
            evrak.Aciklama = txtAciklama.Text;

            List<Line> lines = Session["lines"] as List<Line>;
            int satir = 0;
            foreach (Line item in lines)
            {
                HareketBilgileri hr = new HareketBilgileri();
                hr.BurutTutar = item.Tutar;
                hr.Fiyat = item.Fiyat;
                hr.Isk1Tutar = 0;
                hr.Isk2Tutar = 0;
                hr.Isk3Tutar = 0;
                hr.Isk4Tutar = 0;
                hr.Isk5Tutar = 0;
                hr.KdvOrani = item.KdvOrani;
                hr.KdvSira = item.KdvID;
                hr.KdvTutari = item.Tutar / 100 * item.KdvOrani;
                hr.Miktar = item.Miktar;
                hr.Satir = satir;
                hr.SipDbcNo = 0;
                hr.SipRecNo = 0;
                hr.StokKodu = item.Kodu;
                evrak.Rows.Add(hr);

                satir++;
            }
            bool eski = false;
            if (!string.IsNullOrEmpty(txtSira.Text))
            {
                eski = true;
            }
            mikro.EvrakEkle(evrak, eski);
        }

        protected void btnYeni_Click(object sender, EventArgs e)
        {
            Session["lines"] = null;
            Response.Redirect("/Fatura.aspx");
        }

        protected void txtSira_ButtonClick(object source, DevExpress.Web.ASPxEditors.ButtonEditClickEventArgs e)
        {
            Session["lines"] = null;
            Response.Redirect("/Liste.aspx?tip=fatura");
        }

        protected void btnSil_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtSira.Text))
            {
                mikro.EvrakSil(txtSeri.Text, Convert.ToInt32(txtSira.Text), EvrakTipleri.SatisFaturasi);
                Session["lines"] = null;
                Response.Redirect("/Liste.aspx?tip=cariliste");
            }
        }

        protected void ASPxGridView2_HtmlDataCellPrepared(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableDataCellEventArgs e)
        {
            if (e.DataColumn.FieldName == "Kodu" && e.DataColumn.Caption == "Resim")
            {
                String currentBrand = e.CellValue.ToString();
                e.Cell.Attributes.Add("onclick", "ShowPopup('" + currentBrand + "','" + Convert.ToString(e.Cell.ClientID) + "')");
            }
        }

        protected void ASPxButton2_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Liste.aspx?tip=cariliste");
        }
    }
}